<?php
/**
 * Created by swagger_zxd.
 * User: zxd
 * Date: 2017/11/17
 * Time: 16:04
 */

namespace App\Http\Controllers\NoticeApi;





use App\Http\Requests\Admin\Notice\NoticeCreateRequest;
use App\Http\Requests\Admin\Notice\NoticeEditRequest;
use App\Models\Notice\NoticeInfo;
use Illuminate\Http\Request;

class NoticeController extends BaseController
{

    /**
     * 获取通知列表
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function index(Request $request)
    {
        //获取请求数据
        $param=$request->all();

        //返回响应
        return $this->tool->response($this->noticeService->dataTable(NoticeInfo::class, ['id', 'content', 'times', 'created_at', 'updated_at'], $param, [
            'condition'=>[
                [
                    'where',
                    ['where','content like %?%']
                ]
            ]
        ]), true, 'json');

    }

    /**
     * 创建通知
     * @param NoticeCreateRequest $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(NoticeCreateRequest $request)
    {
        $form_data=$request->except(['_token','__method']);

        $result=$this->noticeService->createNotice($form_data);

        //返回响应
        return $this->tool->response($result,'notice/notice');
    }


    /**
     * 编辑通知页
     * @param $id
     * @param NoticeEditRequest $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update($id, NoticeEditRequest $request)
    {
        $id=(int)$id;

        //获取数据
        $form_data=$request->except(['_token','_method']);

        //z执行修改
        $result=$this->noticeService->editNotice($id, $form_data);

        //返回修改结果
        return $this->tool->response($result,'notice/notice');
    }

    /**
     * @param $id
     */
    public function destroy($id)
    {
        $id=(int)$id;

        //删除通知
        $result=$this->noticeService->deleteNotice($id);

        //返回响应
        return $this->tool->setType('json')->response($result);
    }
}